package com.yima.baseproj.module.sys.select;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yima.baseproj.module.sys.clazz.ClassEntity;
import com.yima.baseproj.module.sys.clazz.ClassService;
import com.yima.baseproj.module.sys.kindergarten.KindergartenEntity;
import com.yima.baseproj.module.sys.kindergarten.KindergartenService;
import com.yima.baseproj.module.sys.student.StudentEntity;
import com.yima.baseproj.module.sys.student.StudentService;
import com.yima.baseproj.module.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/sys/select")
public class KgClassStuSelect {

    @Autowired
    private SysUserService userService;
    @Autowired
    private KindergartenService kindergartenService;
    @Autowired
    private ClassService classService;
    @Autowired
    private StudentService studentService;

    /**
     * 二级联动数据
     * @return
     */
    @RequestMapping("/kgClass")
    public List<Map> kgClassSel(){

        List<Map> kgList = new ArrayList<>();
        //判断当前用户是否绑定幼儿园, 获取获取幼儿园列表
        List<KindergartenEntity> kindergartenEntityList;
        if(userService.findKgIdByUserId()==0){
            kindergartenEntityList = kindergartenService.list(new QueryWrapper<KindergartenEntity>()
                    .eq("del_flag",0));
        }else{
            kindergartenEntityList = kindergartenService.list(new QueryWrapper<KindergartenEntity>()
                    .eq("del_flag",0)
                    .eq("id",userService.findKgIdByUserId()));
        }

        //循环生成map对象, 对应多级联动中的值
        for(KindergartenEntity kg: kindergartenEntityList){
            Map map1 = new HashMap<>();
            map1.put("value",kg.getId());
            map1.put("lable",kg.getKgName());

            List<Map> classList = new ArrayList<>();//用于添加到多级联动中的children
            //获取幼儿园对应的班级列表
            List<ClassEntity> classEntityList = classService.list(new QueryWrapper<ClassEntity>().eq("kg_id",kg.getId()));//根据幼儿园id获取班级列表
            for(ClassEntity cla:classEntityList){//循环,每一个班级封装成map对象, 组成一个数组
                Map map2 = new HashMap<>();
                map2.put("value",cla.getId());
                map2.put("lable",cla.getClassName());
                classList.add(map2);
            }
            map1.put("children",classList);
            kgList.add(map1);

        }
        return kgList;
    }

    /**
     * 三级联动数据
     * @return
     */
    @RequestMapping("/kgClassStu")
    public List<Map> kgClassStuSel(){

        //获取幼儿园列表
        List<Map> kgList = new ArrayList<>();
        //判断当前用户是否绑定幼儿园, 获取获取幼儿园列表
        List<KindergartenEntity> kindergartenEntityList;
        if(userService.findKgIdByUserId()==0){
            kindergartenEntityList = kindergartenService.list(new QueryWrapper<KindergartenEntity>()
                    .eq("del_flag",0));
        }else{
            kindergartenEntityList = kindergartenService.list(new QueryWrapper<KindergartenEntity>()
                    .eq("del_flag",0)
                    .eq("id",userService.findKgIdByUserId()));
        }


        for(KindergartenEntity kg: kindergartenEntityList) {
            Map map1 = new HashMap<>();
            map1.put("value", kg.getId());
            map1.put("lable", kg.getKgName());


            //获取幼儿园对应的班级列表
            List<Map> classList = new ArrayList<>();//用于添加到多级联动中的children
            List<ClassEntity> classEntityList = classService.list(new QueryWrapper<ClassEntity>().eq("kg_id", kg.getId()));//根据幼儿园id获取班级列表
            for (ClassEntity cla : classEntityList) {//循环, 每一个班级封装成map对象, 组成一个数组
                Map map2 = new HashMap<>();
                map2.put("value", cla.getId());
                map2.put("lable", cla.getClassName());

                //获取班级对应的学生列表
                List<Map> stuList = new ArrayList<>();//用于添加到多级联动中的children
                List<StudentEntity> stuEntityList = studentService.list(new QueryWrapper<StudentEntity>().eq("class_id", cla.getId()));//根据幼儿园id获取班级列表
                //循环
                for (StudentEntity stu : stuEntityList) {//循环, 每一个班级封装成map对象, 组成一个数组
                    Map map3 = new HashMap<>();
                    map3.put("value", stu.getId());
                    map3.put("lable", stu.getStudentName());
                    stuList.add(map3);
                }

                map2.put("children",stuList);
                classList.add(map2);
            }
            map1.put("children", classList);
            kgList.add(map1);
        }
        return kgList;
    }


}
